Import LibPoly and utitlities


In [1]:
from sys import stdout
import polypy
import itertools

Table printout utilities


In [2]:
def output(item, width = 15):
  if (isinstance(item, tuple)):
    stdout.write("({:.2f} {:.2f})".format(item[0], item[1]).center(width))
  elif (isinstance(item, float)):
    stdout.write("{:.2f}".format(item).center(width))
  else: 
    stdout.write("{}".format(item).center(width))

Compute and print the sign table

  • x: the main variable
  • polys: the polynomials to sign table
  • m: the model for other variables

In [3]:
def sign_table(x, polys, m):
  # Get the roots and print the header
  roots = set()  # Set of roots
  output("poly/int")
  for f in polys:
    output(f)
    f_roots = f.roots_isolate(m)
    roots.update(f_roots)
  stdout.write("\n")
  # Sort the roots and add infinities
  roots = [polypy.INFINITY_NEG] + sorted(roots) + [polypy.INFINITY_POS]
  # Print intervals and signs in the intervals
  root_i, root_j = itertools.tee(roots)
  next(root_j)
  for r1, r2 in itertools.izip(root_i, root_j):
    output((r1.to_double(), r2.to_double()))
    # The interval (r1, r2)
    v = r1.get_value_between(r2);
    m.set_value(x, v)
    for f in polys: output(f.sgn(m))
    stdout.write("\n")
    # The interval [r2]
    if r2 != polypy.INFINITY_POS:
      output(r2.to_double())
      m.set_value(x, r2)
      for f in polys: output(f.sgn(m))
      stdout.write("\n")
  m.unset_value(x)

Test the sign table on $(x^2 - 2)$ and $(x^2 - 3)$.


In [4]:
x = polypy.Variable('x')

In [5]:
m = polypy.Assignment()

In [6]:
polys = [x**2 - 2, x**2 - 3]

In [7]:
sign_table(x, polys, m)


    poly/int      1*x**2 - 2     1*x**2 - 3  
  (-inf -1.73)        1              1       
     -1.73            1              0       
 (-1.73 -1.41)        1              -1      
     -1.41            0              -1      
  (-1.41 1.41)        -1             -1      
      1.41            0              -1      
  (1.41 1.73)         1              -1      
      1.73            1              0       
   (1.73 inf)         1              1       

In [8]:
y = polypy.Variable('y')
polys_y = [x**2 + y**2 - 1, (x - 1)**2 + y**2 - 1]

In [13]:
polys_x = [x + 1, x + 0, 2*x - 1, x - 1, x - 2]

In [14]:
m = polypy.Assignment()

In [15]:
sign_table(x, polys_x, m)


    poly/int       1*x + 1          1*x          2*x - 1        1*x - 1        1*x - 2    
  (-inf -1.00)        -1             -1             -1             -1             -1      
     -1.00            0              -1             -1             -1             -1      
  (-1.00 0.00)        1              -1             -1             -1             -1      
      0.00            1              0              -1             -1             -1      
  (0.00 0.50)         1              1              -1             -1             -1      
      0.50            1              1              0              -1             -1      
  (0.50 1.00)         1              1              1              -1             -1      
      1.00            1              1              1              0              -1      
  (1.00 2.00)         1              1              1              1              -1      
      2.00            1              1              1              1              0       
   (2.00 inf)         1              1              1              1              1       

In [12]:
m.set_value(x, -1)

In [13]:
sign_table(y, polys_y, m)


    poly/int   1*y**2 + (1*x**2 - 1)1*y**2 + (1*x**2 - 2*x)
  (-inf 0.00)         1              1       
      0.00            0              1       
   (0.00 inf)         1              1       

In [ ]: